package com.testapp.bluetoothtest08.File;

import android.bluetooth.BluetoothAdapter;


import com.testapp.bluetoothtest08.Constant.Constants;

import java.io.File;
import java.io.FileWriter;

public class CodeFile {
    File file = new File("/data/data/com.testapp.bluetoothtest08/Code.txt");
    Params params = new Params();
    BaseFileOperation bfo = new BaseFileOperation();


    public CodeFile(){
        init();
    }
    //初始化内容
    private void init(){
        try{
            if(Constants.key==null){
                params.changeKey();
            }
            if(Constants.LastT==null){
                Constants.LastT=MD5.md5("Name",Constants.key);
                params.writeLastT(Constants.LastT);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //写入
    public void write(String code[][]){
        try {
            FileWriter fileWriter = new FileWriter(file);
            for (String[] strings : code) {
                for (String string : strings) {
                    fileWriter.write(string+";");
                }
            }

            fileWriter.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    //创建
    public void createCode(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    //读取原来的code
                    String temp []=new String[12*14];
                    String tn=params.readLastT();

                    String tmp=read();
                    if(tmp!=null){
                        temp = read().split(";");
                    }
                    System.out.println("读取的temp ----> "+tmp);

                    int index = 0;
                    for (int p = 0; p <14 ; p++) {
                        for (int q = 0; q <12 ; q++) {
                            Constants.code[p][q]=temp[index];
                            index++;
                        }
                    }


                    //读取原来生成码的天数和小时 (位置)
                    int day =params.readDay();
                    int time = params.readTime();
                    //开始继续生成，赋值在相应的位置
                    try {
                        tn =MD5.md5(tn,Constants.key);
                        Constants.code[day][time]=tn;
                        Thread.sleep(2000);
                        write(Constants.code);
                        time++;
                        //判断天数范围和时间范围
                        if(time>=12){
                            time=0;
                            day++;
                        }
                        if(day>=14){
                            day=0;
                        }
                        BluetoothAdapter.getDefaultAdapter().setName(tn);
                        System.out.println("更换设备名称 ----> "+tn);
                        //记录day 、 time 、 tn
                        params.writeDay(day);
                        params.writeTime(time);
                        params.writeLastT(tn);
                        //获取t1
//                        Constants.t1 =Constants.code[day][time];
//                        if(Constants.t1.equals("null")){
//                            Constants.t1 =Constants.code[0][0];
//                        }
                        //
                        new FirstCodeFile().write(Constants.code[day][time]);
                        if(new FirstCodeFile().read().equals("null")||new FirstCodeFile().read()==null||new FirstCodeFile().read().equals("")){
                            new FirstCodeFile().write(Constants.code[0][0]);
                        }
                        //
                        System.out.println("Constants.code[0][0] ----> "+Constants.code[0][0]);
                        System.out.println("Constants.t1 ----> "+Constants.t1);

                        Thread.sleep(60*60*1000);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();
    }
    //读取编码
    public String read() {
        return  bfo.readString(file);
    }

    //清空
    public void clearCode(){
        bfo.write(file,"");
    }


    public void createCodeOne(){
        //读取原来的code
        String temp []=new String[12*14];
        String tn=params.readLastT();

        String tmp=read();
        if(tmp!=null){
            temp = read().split(";");
        }
        System.out.println("读取的temp ----> "+tmp);

        int index = 0;
        for (int p = 0; p <14 ; p++) {
            for (int q = 0; q <12 ; q++) {
                Constants.code[p][q]=temp[index];
                index++;
            }
        }


        //读取原来生成码的天数和小时 (位置)
        int day =params.readDay();
        int time = params.readTime();
        //开始继续生成，赋值在相应的位置
        try {
            tn =MD5.md5(tn,Constants.key);
            Constants.code[day][time]=tn;
            Thread.sleep(2000);
            write(Constants.code);
            time++;
            //判断天数范围和时间范围
            if(time>=12){
                time=0;
                day++;
            }
            if(day>=14){
                day=0;
            }
            BluetoothAdapter.getDefaultAdapter().setName(tn);
            System.out.println("更换设备名称 ----> "+tn);
            //记录day 、 time 、 tn
            params.writeDay(day);
            params.writeTime(time);
            params.writeLastT(tn);
            //获取t1
//                        Constants.t1 =Constants.code[day][time];
//                        if(Constants.t1.equals("null")){
//                            Constants.t1 =Constants.code[0][0];
//                        }
            //
            if(Constants.code[day][time]==null||Constants.code[day][time].equals("")){
                new FirstCodeFile().write(Constants.code[0][0]);
            }else{
                new FirstCodeFile().write(Constants.code[day][time]);
            }

            System.out.println("read CodeFirst ----> "+new FirstCodeFile().read());
            //
            System.out.println("Constants.code[0][0] ----> "+Constants.code[0][0]);
            System.out.println("Constants.t1 ----> "+Constants.t1);


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
